package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.BasicRoutingProcess;
import com.futureCloudMOM.core.service.BasicRoutingProcessService;
import com.futureCloudMOM.core.mapper.BasicRoutingProcessMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 【Basic_RoutingProcess】的数据库操作Service实现
 *
 * @author Administrator
 * @since 2023-06-06 15:19:30
 */
@Service
public class BasicRoutingProcessServiceImpl extends BaseServiceImpl<BasicRoutingProcessMapper, BasicRoutingProcess> implements BasicRoutingProcessService {

    @Override
    public List<BasicRoutingProcess> selectListByRoutingId(Long routingId) {
        return list(new LambdaQueryWrapper<BasicRoutingProcess>()
                .eq(BasicRoutingProcess::getRoutingId, routingId));
    }

    @Override
    public Long getProcessTime(Long id, BigDecimal qty, Integer cavityQty) {
        BasicRoutingProcess routingProcess = getById(id);
        BigDecimal stdTime;
        if (ObjectUtils.isEmpty(routingProcess) || ObjectUtils.isEmpty(stdTime = routingProcess.getStdTime())) {
            return null;
        }
        if (ObjectUtils.isEmpty(qty)) {
            return null;
        }
        if (ObjectUtils.isEmpty(cavityQty) || cavityQty == 0) {
            throw new BaseException("工序加工时长计算，模具穴数不能为0");
        }
        return qty.multiply(stdTime).divide(BigDecimal.valueOf(cavityQty), RoundingMode.CEILING).longValue();
    }

}
